package com.game.view.layer
{
    import com.dusk.util.ResUtil;
    import com.dusk.util.StringUtil;
    import com.game.manager.GameSceneManager;
    import com.game.manager.NotificationManager;
	import com.game.manager.SoundManager;
	
	import flash.display.Bitmap;
    import flash.display.DisplayObject;
    import flash.display.MovieClip;
    import flash.display.SimpleButton;
    import flash.display.Sprite;
    import flash.events.MouseEvent;
    
    public class SelectRole extends LayerBase
	{
		
		public function SelectRole()
		{
			super();
		}
		
		private var btn1:SimpleButton;
		private var btn2:SimpleButton;
		private var btn3:SimpleButton;
		private var btn4:SimpleButton;
		private var btn5:Sprite;
		private var startBtn:SimpleButton;
		private var backBtn:SimpleButton;
		private var resetBtn:SimpleButton;
		
		private var selectedId:Array = [];
		
		private const totalNum:int = 4;
		private var upStateBackup:Vector.<DisplayObject> = new <DisplayObject>[];
		
		public function showCartoon():void
		{
			var cartoon:GameCartoon = new GameCartoon();
			cartoon.show("造1开场",GameCartoon.SHOW_MAP);
		}
		
		override protected function beforeAdded():void
		{
			var mc:MovieClip = ResUtil.getLayerView("SelectRole") as MovieClip;
			btn1 = mc.getChildByName("btn1") as SimpleButton;
			btn2 = mc.getChildByName("btn2") as SimpleButton;
			btn3 = mc.getChildByName("btn3") as SimpleButton;
			btn4 = mc.getChildByName("btn4") as SimpleButton;
			btn5 = mc.getChildByName("btn5") as Sprite;
			startBtn = mc.getChildByName("startBtn") as SimpleButton;
			backBtn = mc.getChildByName("backBtn") as SimpleButton;
			resetBtn = mc.getChildByName("resetBtn") as SimpleButton;
			addChild(mc);
		}
		
		override protected function onAdded():void
		{
			for(var i:int = 1; i <= totalNum; i++)
			{
				this["btn" + i].addEventListener(MouseEvent.CLICK, this.onClick);
				upStateBackup.push((this["btn" + i] as SimpleButton).upState);
			}
			btn5.addEventListener(MouseEvent.CLICK, NotificationManager.tellNotOpen);
			startBtn.addEventListener(MouseEvent.CLICK, startGame);
			backBtn.addEventListener(MouseEvent.CLICK, backMenu);
			resetBtn.addEventListener(MouseEvent.CLICK, resetRole);
		}
		
		override protected function onRemoved():void
		{
			for(var i:int = 1; i <= totalNum; i++)
			{
				this["btn" + i].removeEventListener(MouseEvent.CLICK, onClick);
			}
			btn5.removeEventListener(MouseEvent.CLICK,NotificationManager.tellNotOpen);
			startBtn.removeEventListener(MouseEvent.CLICK, startGame);
			backBtn.removeEventListener(MouseEvent.CLICK, backMenu);
			resetBtn.removeEventListener(MouseEvent.CLICK, resetRole);
			upStateBackup = null;
		}
		
		private function startGame(evt:MouseEvent):void
		{
			SoundManager.playSound("Select");
			if(selectedId.length != gc.playNum)
			{
				NotificationManager.alert("请先选择角色");
				return;
			}
			gc.playerData.roleId = selectedId[0] ? selectedId[0] : 0;
			showCartoon();
			removeFromParent();
		}
		
		private function backMenu(evt:MouseEvent):void
		{
			removeFromParent();
			GameSceneManager.getIns().switchScene(GameSceneManager.GAME_MENU);
		}
		
		private function resetRole(evt:MouseEvent):void
		{
			for(var i:int = 1; i <= totalNum; i++)
			{
				(this["btn" + i] as SimpleButton).upState = upStateBackup[i - 1];
				(this["btn" + i] as SimpleButton).enabled = true;
			}
			if(getChildByName("arrow1") != null) removeChild(getChildByName("arrow1"));
			if(getChildByName("arrow2") != null) removeChild(getChildByName("arrow2"));
			selectedId = [];
		}
		
		private function onClick(param1:MouseEvent):void
		{
			if(gc.playNum == selectedId.length)
			{
				return;
			}
			var bm:Bitmap = ResUtil.getImageBitmap((selectedId.length + 1).toString() + "P");
			bm.name = "arrow" + (selectedId.length + 1).toString();
			bm.x = param1.currentTarget.x - 50;
			bm.y = 40;
			addChild(bm);
			
			var id:int = int(StringUtil.getLastMultiChars(param1.currentTarget.name));
			selectedId.push(id);
			param1.target.upState = param1.target.downState;
			if(gc.playNum == selectedId.length)
			{
				for(var i:int = 1; i <= totalNum; i++)
				{
					(this["btn" + i] as SimpleButton).enabled = false;
				}
			}
		}
	}
}
